/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.sharedbills.shared.model;

import org.jb.sharedbills.shared.model.Group.Kind;
import org.jb.sharedbills.shared.model.notification.AbstractNotifier;
import org.jb.sharedbills.shared.model.notification.ObservableCollection;
import org.jb.sharedbills.shared.model.notification.ObservableList;

public class Event extends AbstractNotifier {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4565181403573746825L;
	private String name;
	private ObservableList<Member> members = new ObservableList<Member>( this, "members" );
	private ObservableList<Group> groups = new ObservableList<Group>( this, "groups" );
	private ObservableList<Bill> bills = new ObservableList<Bill>( this, "bills" );
	
	public Event() {
		Group group = new Group();
		group.setKind(Kind.ALL_MEMBERS);
		group.setName("Complet");
		
		groups.add( group );
	}
	
	/**
	 * 
	 * @return
	 */
	public ObservableCollection<Member> getMembers() {
		return members;
	}
	
	/**
	 * 
	 * @param name
	 * @return
	 */
	public Member getMemberByName( String name ) {
		for( Member member : members ) {
			if (name.equals(member.getName()))
				return member;
		}
		return null;
	}
	
	/**
	 * 
	 * @return
	 */
	public ObservableCollection<Bill> getBills() {
		return bills;
	}
	
	/**
	 * 
	 * @return
	 */
	public ObservableCollection<Group> getGroups() {
		return groups;
	}
	
	/**
	 * 
	 * @return
	 */
	public Group getAllMembersGroup() {
		for( Group group : groups ) {
			if (group.getKind() == Kind.ALL_MEMBERS) {
				return group;
			}
		}
		
		return null;
	}
	
	/**
	 * 
	 * @param name
	 */
	public void setName(String name) {
		if (this.name == null || !this.name.equals(name)) {
			String oldName = this.name;
			this.name = name;
			doNotify( "name", oldName, this.name );
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public String getName() {
		return name;
	}
}
